﻿using System;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;

namespace Common.Util
{
    public class Encrypt
    {
        // This function will encrypt a string
        public static string EncryptMD5(string strInput, string strKey)
        {
            MD5 md5Hash = MD5.Create();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(strInput + strKey));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            var sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        // This function will encrypt password
        public static string EncryptPassword(string strInput)
        {
            return EncryptMD5(strInput, "keypassword");
        }

        // Create random password
        public static string GetRandomPassword()
        {
            var obj = new Random();
            return obj.Next(1000, 9999).ToString(CultureInfo.InvariantCulture);
        }

        // Create random password 6 numbers
        public static string GetRandomPassword2()
        {
            var obj = new Random();
            return obj.Next(100000, 999999).ToString(CultureInfo.InvariantCulture);
        }
    }
}
